//
//  ViewController.m
//  fmdb
//
//  Created by song on 15/9/7.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "FMDB.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    NSString *path =[[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject]
                     stringByAppendingString:@"song.sqlite"];
    
    FMDatabaseQueue *queue =[FMDatabaseQueue databaseQueueWithPath:path];
    
    [queue inDatabase:^(FMDatabase *db) {
        FMResultSet *rs =[db executeQuery:@"select * from t_stutent" ];
        
        while (rs.next) {
            
            int ID= [rs intForColumn:@"id"];
            NSString *name =[ rs stringForColumn:@"name"];
            int age = [rs intForColumn:@"age"];
            NSLog(@"%d  %@ %d" , ID , name , age);
            
        }
    }];
    [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
    }];//再一个事务中操作数据库
    
    
    

}

-(void)basicFMDB
{
    
    NSString *path =[[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject]
                     stringByAppendingString:@"song.sqlite"];
    

    FMDatabase *db = [FMDatabase databaseWithPath:path];
    
    if([db open])
    {
        
        BOOL result = [db executeUpdate:@"create table if not exsits t_stutent(id integer primary key autoincrement, name text ,age integer);"];
        if(result)
        {
            NSLog(@"成功创表");
        }
        [db executeUpdate:@"begin transaction"];        //开始事务
        [db executeUpdate:@"insert into t_stutent(name ,age) values (?, ?)" ,@"hello word",@20];
        [db executeUpdate:@"commit transaction"];       // 提交事务
        FMResultSet *rs =[db executeQuery:@"select * from t_stutent" ];
        
        while (rs.next) {
            
            int ID= [rs intForColumn:@"id"];
            NSString *name =[ rs stringForColumn:@"name"];
            int age = [rs intForColumn:@"age"];
            NSLog(@"%d  %@ %d" , ID , name , age);
            
        }
    }
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
